<?php

include_once('conn.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file_upload'])) {
    $upload_dir = 'uploads/'; // 确保这个目录已经存在并且可写
    $upload_file = $upload_dir . basename($_FILES['file_upload']['name']);
    //print_r()
    if(!empty($_FILES['img_upload']['name'])){
        $upload_img = $upload_dir . basename($_FILES['img_upload']['name']);
    }else{
        $upload_img = "uploads/default.png";
    }
    //获取值
    $names = $_POST['names'];
    $tab = $_POST['tab'];
    $catalog = $_POST['catalog'];
    if (move_uploaded_file($_FILES['file_upload']['tmp_name'], $upload_file)) {
        $sql = "INSERT INTO video (name,catalog,img,url,tab,is_del) VALUES ('".$names."','".$catalog."','".$upload_img."','".$upload_file."','".$tab."',1)";
        $query = mysqli_query($conn,$sql);
        echo "文件上传成功！";
    } else {
        print_r($sql);
        echo "文件上传失败！";
    }
} else {
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
    <div>选择视频：<input type="file" name="file_upload"></div>
    <div>选择封面：<input type="file" name="img_upload"></div>
    <div>选择分类：
    <select id="catalog" name="catalog">
    <option value="0">请选择</option>
        <?php
            $sql0 = "SELECT * FROM catalog";
            $query0 = mysqli_query($conn,$sql0);
            while($row = mysqli_fetch_array($query0)){
        ?>
                <option value="<?php echo $row['id']?>"><?php echo $row['name']?></option>
        <?php
            }
        ?>
    </select>
    </div>
    <div>视频名称：<input type="text" name="names" value=""></div>
    <div>视频标签：<input type="text" name="tab" value=""></div>
    <input type="submit" value="上传">
</form>
<?php
}
?>